package com.itheima.health.h5.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.omg.CORBA.TIMEOUT;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: health_paret
 * @description: 前端订单
 * @author: Mr.Wang
 * @create: 2020-11-12 20:06
 **/
@RestController
@RequestMapping("order")
@Api(tags = "移动端的预约功能")
public class OrdeController {
    @Reference
    private OrderService orderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;//这个的话是AmqpTemplate的实现类
    @ApiOperation(value = "短信发送", notes = "从前端获得需要发的手机号进行短信发送")
    @GetMapping("sendCode/{telephone}")
    public Result sendCodePhone(@PathVariable("telephone") String telephone) {
        String code = ValidateCodeUtils.generateValidateCode(4);
        //这里是把map集合当作参数发了过去
        HashMap<Object, Object> map = new HashMap<>();
        map.put("telephone", telephone);
        map.put("code", code);

        //map.put("shoujihao", telephone);
        rabbitTemplate.convertAndSend("bocai1",map);//将map放到消息队列中
        //发送验证码
        //SmsUtils.validUserTelephone(telephone, code);
        System.out.println("验证码是：" + code);
        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER + telephone, code, 30, TimeUnit.SECONDS);
        return new Result(true, "验证码发送失败", code);
    }

    @PostMapping("add")
    public Result add(@RequestBody Map map) {
        Order add = orderService.add(map);
        return new Result(true,"预约失败",add);
    }

    @GetMapping("findOrderInfoByOrderId/{id}")
    @ApiOperation(value = "预约成功", notes = "根据orderid来查询详情")
    public Result findOrderInfoByOrderId(@PathVariable("id") int id) {
        return new Result(orderService.findOrderInfoByOrderId(id));
    }
}


